package com.yl.androidutilscenter.utils.ext

import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import java.lang.reflect.ParameterizedType

/**
 * @author
 * @description:扩展函数
 * @date :2024/3/8
 * @version 1.0.0
 */

/**
 * 获取当前类绑定的泛型ViewModel-clazz
 */
@Suppress("UNCHECKED_CAST")
fun <VM> getVmClazz(obj: Any): VM {
//    return
//    if( obj.javaClass.genericSuperclass is ParameterizedType){
    return (obj.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as VM
//    }else{
//        null
//    }
}

/**
 * 创建flow流，发送一次数据
 */
fun <T> getFlow(block: suspend () -> T): Flow<T> {
    return flow {
        emit(block())
    }
}

/**
 * 创建flows，多次发送事件
 */
fun <T> getFlows(vararg blocks: suspend () -> T): Flow<T> {
    return flow {
        blocks.forEach { block ->
            emit(block())
        }
    }
}


/**
 * 关闭软键盘
 */
fun Activity.hideInput() {
    val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    val v = this.window.peekDecorView()
    if (null != v) {
        imm.hideSoftInputFromWindow(v.windowToken, 0)
    }
}








